package ysq.xyg.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.web.dao.WebProductDao;
import ysq.xyg.web.model.po.WebProduct;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.vo.WebProductVo;
import ysq.xyg.web.service.WebProductService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 网站模块-商品信息service实现
 *
 * @author shengQiang yu
 * @date 2019-6-7 18:07:28
 */
@Service("webProductService")
public class WebProductServiceImpl extends BaseServiceImpl<WebProduct> implements WebProductService {

    @Resource
    private WebProductDao webProductDao;

    private WebProduct webProduct;

    @Autowired
    public void setBaseDao(WebProductDao webProductDao) {
        this.baseDao = webProductDao;
    }


    /**
     * 分页查询
     *
     * @param webProductVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-6-7 18:07:28
     */
    public ReturnEntity page(WebProductVo webProductVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<WebProduct> list = acceptList(webProductVo);
        PageInfo<WebProduct> page=new PageInfo<WebProduct>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param webProductVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-6-7 18:07:28
     */
    public ReturnEntity addOrUpdate(WebProductVo webProductVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(webProductVo.getId())){
            webProduct=new WebProduct(currentUser);
            isUpate=false;
        }else{
            webProduct=selectByPrimaryKey(webProductVo.getId());
            if(ObjectUtil.isNull(webProduct)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            webProduct.setUpdateTime(new Date());
            webProduct.setUpdateUser(currentUser.getId());
        }
        webProduct.setFristType(webProductVo.getFristType());
        webProduct.setName(webProductVo.getName());
        webProduct.setColId(webProductVo.getColId());
        webProduct.setScore(webProductVo.getScore());
        webProduct.setImages(webProductVo.getImages());
        webProduct.setCoverImage(webProductVo.getCoverImage());
        webProduct.setSaleNum(webProductVo.getSaleNum());
        webProduct.setDescription(webProductVo.getDescription());
        webProduct.setContent(webProductVo.getContent());
        webProduct.setSort(webProductVo.getSort());
        webProduct.setShowType(webProductVo.getShowType());
        webProduct.setUserId(currentUser.getId());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(webProduct);
        }else{
            num=insert(webProduct);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-6-7 18:07:28
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        WebProduct webProduct=selectByPrimaryKey(id);
        if(webProduct!=null){
            webProduct.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            webProduct.setUpdateTime(new Date());
            webProduct.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(webProduct);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param webProductVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-6-7 18:07:28
     */
    public List<WebProduct> acceptList(WebProductVo webProductVo){
        Example example = new Example(WebProduct.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(webProductVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",webProductVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(webProductVo.getColId())){
            criteria.andEqualTo("colId",webProductVo.getColId());
        }
        if(StringUtil.isNotEmpty(webProductVo.getName())){
            criteria.andLike("name","%"+webProductVo.getName()+"%");
        }
        if(StringUtil.isNotEmpty(webProductVo.getShowType())){
            criteria.andEqualTo("showType",webProductVo.getShowType());
        }
        if(StringUtil.isNotEmpty(webProductVo.getFristType())){
            criteria.andEqualTo("fristType",webProductVo.getFristType());
        }
        if(StringUtil.isNotEmpty(webProductVo.getUserId())){
            criteria.andEqualTo("userId",webProductVo.getUserId());
        }
        example.setOrderByClause(" sort desc,update_time desc");
        return webProductDao.selectByExample(example);
    }
}
